
local w0,h0 = 322,244
local ui = class.panel:builder 
{
    x = 1920 - w0,
    y = 300,
    w = w0,
    h = h0,
    normal_image = [[ui\排行榜\面板.tga]],
}
--ui:add_text('排行榜',0,5,w0,25,12,5)

local panels = {}
local size = 5
local w = 291
local h = 40
for a=1,4 do
    local panel = ui:add_panel([[ui\排行榜\条.tga]],15,(a-1)*(h+5)+50,w-40,h)
    panel.icon = panel:add_panel([[ReplaceableTextures\CommandButtons\BTNSkeletonArcher.blp]],panel.w,0,h,h)
    panel.load1 = panel:add_panel([[ReplaceableTextures\TeamColor\TeamColor05.blp]],5,5,panel.w*0.5,panel.h-10)
    panel.load2 = panel:add_panel([[core\hpbar\蓝条.tga]],5,5,panel.w*0.5,panel.h-10)
    panel.dps = panel:add_text('999999',10,0,panel.w,h,12,4)
    panel.name = panel:add_text('玩家'..a,0,0,panel.w-5,nil,12,6)
    panel.load_w = panel.w - 10

    --panel.dps:set_color(0xcffffff00)
    panels[a] = panel
end

function ui:up_date(unit)
    local list = unit:get_data('伤害表') or {}
    local max = 0
    for a,panel in ipairs(panels) do
        local player = ac.player[a]
        local info = list[a] or {}
        local dps1 = info['总伤害'] or 0
        max = math.max(max,dps1)
    end
    for a,panel in ipairs(panels) do
        local player = ac.player[a]
        local hero = player.hero
        local info = list[a] or {}
        local dps1 = info['总伤害'] or 0
        local dps2 = info['物理伤害'] or 0
        local dps3 = info['魔法伤害'] or 0
        local rate1 = 0
        local rate2 = 0
        local rate3 = max>0 and math.min(dps1/max,1) or 0
        if dps1>0 then
            rate1 = dps2/dps1 * rate3
            rate2 = dps3/dps1 * rate3
        end
        panel.load1:set_width(rate1*panel.load_w+0.01)
        panel.load1:set_position((1 - rate3)*panel.load_w+5,panel.load1.y)
        panel.load2:set_width(rate2*panel.load_w+0.01)
        panel.load2:set_position((1 - rate3 + rate1)*panel.load_w+5,panel.load2.y)
        panel.name:set_text(player:get_name())
        panel.dps:set_text(format_number1(dps1))
        if hero then
            panel.icon:set_normal_image(hero:get_slk("Art", ""))
        end
    end
end

ui:hide()
ac.loop(30,function()
    local player = ac.player.self
    local time = player.show_dps or 0
    local unit = player.dps_unit
    if time>ac.clock() and unit then
        ui:up_date(unit)
        ui:show()
    else
        ui:hide()
    end
end)


local mt = ac.buff['排行榜']

function mt:on_add()
    local list = {}
    self.target:set_data('伤害表',list)
    self:gc(self.target:event '单位-受到伤害'(function(_,target,source,data)
        local player = source.owner
        local info = list[player.id] or {
            ['总伤害'] = 0,
            ['物理伤害'] = 0,
            ['魔法伤害'] = 0,
        }
        list[player.id] = info
        info['总伤害'] = info['总伤害'] + data.real_damage
        if data.damage_type=='物理' then
            info['物理伤害'] = info['物理伤害'] + data.real_damage
        else
            info['魔法伤害'] = info['魔法伤害'] + data.real_damage
        end
        player.show_dps = ac.clock() + 10000
        player.dps_unit = self.target
    end))
end
